package com.netrust.cmsx.admin.modules.sys.web;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.netrust.cmsx.admin.modules.sys.common.AStatic;
import com.netrust.cmsx.admin.modules.sys.pojo.MenuBto;
import com.netrust.cmsx.admin.modules.sys.pojo.MenuPo;
import com.netrust.cmsx.admin.modules.sys.service.MenuService;
import com.netrust.cmsx.admin.modules.sys.utils.UserUtils;
import com.netrust.cmsx.core.config.Global;
import com.netrust.cmsx.core.persist.TreeUtil;
import com.netrust.cmsx.core.resp.RespJson;
import com.netrust.cmsx.core.web.BaseController;
import com.netrust.cmsx.entity.module.sys.Dict;
import com.netrust.cmsx.entity.module.sys.Menu;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


/**
 * 栏目Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService menuService;
	
	@ResponseBody
	@RequestMapping(value = {""})
	public Menu get(@RequestParam(required=false) String id) {
		if (id!=null){
			return menuService.findOne(id);
		}else{
			return new Menu();
		}
	}
	
	@RequestMapping(value = {"form"})
	public String form(@RequestParam(required=false) String id,String op,Model model) {
		Menu menu=new Menu();
		if("update".equals(op)){//代表修改跳转
			menu=menuService.findOne(id);	
			model.addAttribute("pid", menu.getParent().getId());
		}else{
			model.addAttribute("pid", id);
		}
		
		model.addAttribute("menu", menu);
		return "modules/sys/menuForm";
	}
	
	@RequestMapping(value = {"save"})
	public String save(Menu menu) {		
		menu.setType("menu");
		menuService.saveMenu(menu);
		
		return "modules/sys/menuList";
	}
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = {"del"})
	public RespJson del(@RequestParam(required=true) String id) {
		try {
			menuService.deleteInFlag(new Menu(id));
			return new RespJson(Global.Operate_Success, 200);
		} catch (Exception e) {
			return new RespJson(Global.Operate_Error, 500, null);
		}
	}

	@ResponseBody
	@RequestMapping(value = {"list"})
	public List<MenuPo> list() {
//		List<MenuPo> list =menuService.findTreeMenu4User(UserUtils.getPrincipal().getId());
		List<Menu> menuList=menuService.findByType(AStatic.mtype_menu);
		List<MenuPo> menuPos=Lists.newArrayList();
		for (Menu menu : menuList) {
			MenuPo mp=new MenuPo();
			BeanUtils.copyProperties(menu, mp,"parent","children");
			
			mp.setExpanded(true);
			mp.setIsLeaf(true);
			
			String[] pids=mp.getPids().split(",");
			mp.setLevel(pids.length-2);
			
			if(!"1".equals(menu.getId())){
				menuPos.add(mp);
			}
		}
		return menuPos;
	}
	
	@ResponseBody
	@RequestMapping(value = {"treeBot"})
	public List<MenuBto> treeBootstrap() {
		List<Menu> menuList=menuService.findByType(AStatic.mtype_menu);
		
		List<MenuBto> menuBtos=Lists.newArrayList();
		for (Menu menu : menuList) {
			MenuBto mb=new MenuBto();
			BeanUtils.copyProperties(menu, mb,"parent");
			if(menu.getParent()!=null){
				mb.setPid(menu.getParent().getId());				
			}else{
				mb.setPid("0");
			}
			menuBtos.add(mb);
		}
		
		List<MenuBto> mps=new TreeUtil<MenuBto>().getChildTree(menuBtos,1);
		return mps;
	}
}
